package org.arain.baseii.system.organization;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.arain.baseii.aspect.annotation.SystemExceptionLog;
import org.arain.baseii.aspect.annotation.SystemServiceLog;
import org.arain.baseii.common.CommonServiceImpl;
import org.arain.baseii.mapper.SysOrganizationMapper;
import org.arain.baseii.pojo.SysOrganization;
import org.arain.baseii.pojo.SysOrganizationExample;
import org.arain.baseii.utils.AppUtils;
import org.arain.baseii.utils.ArainResult;
import org.arain.baseii.utils.IDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
* <p>Title:OrganizationServiceImpl </p>
* @author Arain
* @date2017年9月4日
 */
@Service
@Transactional
public class OrganizationServiceImpl extends CommonServiceImpl<OrganizationDto> implements OrganizationService{
	@Autowired
	private SysOrganizationMapper sysOrganizationMapper;
	@Override
	public String load_organization() {
		SysOrganizationExample example = new SysOrganizationExample();
		JSONArray array = new JSONArray();
		example.createCriteria().andPidEqualTo((long) 510000);
		List<SysOrganization> list = sysOrganizationMapper.selectByExample(example);
		for (SysOrganization sysOrganization : list) {
			JSONObject object = new JSONObject();
			object.accumulate("name", sysOrganization.getName());
			object.accumulate("value", sysOrganization.getId());
			
			array.add(object);
		}
		return array.toString();
	}
	@Override
	public String load_datagrid(OrganizationDto dto) {
		if(StringUtils.isNotBlank(dto.getCity())) {
			com.alibaba.fastjson.JSONArray array = new com.alibaba.fastjson.JSONArray();
			List<SysOrganization> list = sysOrganizationMapper.selectByFunctionGetChildLst(Long.valueOf(dto.getCity()));
			for (SysOrganization sysOrganization : list) {
				JSONObject object = new JSONObject();
				object.put("id", sysOrganization.getId());
				object.put("text", sysOrganization.getName());
				if (sysOrganization.getPid() != Long.valueOf(dto.getCity())) {
					object.put("pid", sysOrganization.getPid());
				}
				array.add(object);
			}
			return array.toString();
		} else {
			com.alibaba.fastjson.JSONArray array = new com.alibaba.fastjson.JSONArray();
			SysOrganizationExample example = new SysOrganizationExample();
			example.createCriteria().andEnabledEqualTo((short) 1);
			List<SysOrganization> list = sysOrganizationMapper.selectByExample(example);
			for (SysOrganization sysOrganization : list) {
				JSONObject object = new JSONObject();
				object.put("id", sysOrganization.getId());
				object.put("text", sysOrganization.getName());
				if (sysOrganization.getPid() != 0) {
					object.put("pid", sysOrganization.getPid());
				}
				array.add(object);
			}
			return array.toString();
		}
	}
	
	
	@Override
	public String load_pid(OrganizationDto dto) {
		com.alibaba.fastjson.JSONArray array = new com.alibaba.fastjson.JSONArray();
		SysOrganizationExample example = new SysOrganizationExample();
		example.createCriteria().andEnabledEqualTo((short) 1);
		List<SysOrganization> list = sysOrganizationMapper.selectByExample(example);
		for (SysOrganization sysOrganization : list) {
			JSONObject object = new JSONObject();
			object.put("id", sysOrganization.getId());
			object.put("text", sysOrganization.getName());
			if (sysOrganization.getPid() != 0) {
				object.put("pid", sysOrganization.getPid());
			}
			
			array.add(object);
		}
		return array.toString();
	}
	@SystemExceptionLog(description="保存")
	@SystemServiceLog(description="保存")
	@Override
	public ArainResult save_01(OrganizationDto dto) {
		if(StringUtils.isNotBlank(dto.getId())) {
			SysOrganization organization = new SysOrganization();
			AppUtils.copyProperties(organization, dto);
			sysOrganizationMapper.updateByPrimaryKey(organization);
			return ArainResult.ok();
		} else {
			dto.setId(IDUtil.genId());
			SysOrganization organization = new SysOrganization();
			AppUtils.copyProperties(organization, dto);
			sysOrganizationMapper.insert(organization);
			return ArainResult.ok();
		}
		
	}
	@Override
	public OrganizationDto load_01(OrganizationDto dto) {
		SysOrganization organization = sysOrganizationMapper.selectByPrimaryKey(Long.valueOf(dto.getId()));
		AppUtils.copyProperties(dto, organization);
		return dto;
	}

}
